package com.zhao.modleclass.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhao.modleclass.Helper.BitmapCircleTransformation;
import com.zhao.modleclass.R;
import com.zhao.modleclass.bean.HomePageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static android.content.Context.LAYOUT_INFLATER_SERVICE;

public class HP_LiveAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<HomePageBean.TodayLiveVideoBean> mList;
    private List<Map<Integer, Map<Integer, HomePageBean.TodayLiveVideoBean>>> myList = new ArrayList<>();
    public static final int TYPE0 = 0, TYPE1 = 1, TYPE2 = 2;
    private int preLiveConut = 0;
    private boolean flag = true;
    private int size = 1;
    private int count = 0;

    public HP_LiveAdapter(Context context, List<HomePageBean.TodayLiveVideoBean> list) {
        this.context = context;
        this.mList = list;
        Map<Integer, HomePageBean.TodayLiveVideoBean> map =new HashMap<>();
        Map<Integer, Map<Integer, HomePageBean.TodayLiveVideoBean>> mapMap =new HashMap<>();

        for (int i = 0; i < mList.size(); i++) {
            //getLiveType=1为直播
            if (mList.get(i).getLiveType() == 1) {
                map=new HashMap<>();
                mapMap=new HashMap<>();

                map.put(1, mList.get(i));
                mapMap.put(1, map);
                myList.add(mapMap);
                //接口变化后改动 getLiveType() == 2
            } else if (mList.get(i).getLiveType() == 2) {
                preLiveConut++;
            }
        }
        if (preLiveConut % 2 == 1) {
            for (int i = 0; i < mList.size(); i++) {
                if (mList.get(i).getLiveType() == 2) {
                    if (flag) {
                        map=new HashMap<>();
                        mapMap=new HashMap<>();
                        map.put(1, mList.get(i));
                        mapMap.put(0, map);
                        myList.add(mapMap);
                        flag = false;
                    } else if (flag==false){
                        if (count==0||count > 2) {
                            count = 1;
                            map=new HashMap<>();
                            mapMap=new HashMap<>();
                        }
                        map.put(count, mList.get(i));
                        count++;
                        size++;
                        if (size > 2) {
                            mapMap.put(2, map);
                            myList.add(mapMap);
                            size = 1;
                        }
                    }
                }
            }
        } else if (preLiveConut != 0 && preLiveConut % 2 == 0) {
            map=new HashMap<>();
            mapMap=new HashMap<>();
            for (int i = 0; i < mList.size(); i++) {
                if (mList.get(i).getLiveType() == 2) {
                    //新的改动 之前为if（count>2）
                    if (count==0||count > 2) {
                        count = 1;
                        map=new HashMap<>();
                        mapMap=new HashMap<>();
                    }
                    map.put(count, mList.get(i));
                    count++;
                    size++;
                    if (size > 2) {
                        mapMap.put(2, map);
                        myList.add(mapMap);
                        size = 1;
                    }
                }
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        Set<Integer> set = myList.get(position).keySet();
        Iterator<Integer> iterator = set.iterator();
        int p = iterator.next();
        return p;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = null;
        switch (viewType) {
            case TYPE0:
                //大图预告
                view = inflater.inflate(R.layout.item1_hp_live, parent, false);
                return new MyViewHolder0(view);
            case TYPE1:
                //直播
                view = inflater.inflate(R.layout.item2_hp_live, parent, false);
                return new MyViewHolder1(view);
            case TYPE2:
                //小图预告
                view = inflater.inflate(R.layout.item3_hp_live, parent, false);
                return new MyViewHolder2(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder0) {
            HomePageBean.TodayLiveVideoBean bean = myList.get(position).get(TYPE0).get(1);
            Glide.with(context).load(bean.getImageUrl()).into(((MyViewHolder0) holder).iv_item1_trailerPic);
            Glide.with(context).load(bean.getTechHeadUrl())
                    .transform(new BitmapCircleTransformation(context))
                    .into(((MyViewHolder0) holder).iv_item1_trailerIcon);
            ((MyViewHolder0) holder).tv_item1_trailerTxt.setText("#预告/" + bean.getStartTime());
            ((MyViewHolder0) holder).tv_item1_TeacherName.setText(bean.getTechNickName());
            ((MyViewHolder0) holder).tv_item1_TeacherPosition.setText(bean.getTechJobName());
            ((MyViewHolder0) holder).tv_item1_Title.setText(bean.getTitle());
            ((MyViewHolder0) holder).tx_item1_Detail.setText(bean.getDesc());
        }
        if (holder instanceof MyViewHolder1) {
            HomePageBean.TodayLiveVideoBean bean = myList.get(position).get(TYPE1).get(1);
            Glide.with(context).load(bean.getImageUrl()).into(((MyViewHolder1) holder).iv_item2_trailerPic);
            Glide.with(context).load(bean.getTechHeadUrl())
                    .transform(new BitmapCircleTransformation(context))
                    .into(((MyViewHolder1) holder).iv_item2_trailerIcon);
            ((MyViewHolder1) holder).tv_item2_TeacherName.setText(bean.getTechNickName());
            ((MyViewHolder1) holder).tv_item2_TeacherPosition.setText(bean.getTechJobName());
            ((MyViewHolder1) holder).tv_item2_Title.setText(bean.getTitle());
            ((MyViewHolder1) holder).tx_item2_Detail.setText(bean.getDesc());

        }
        if (holder instanceof MyViewHolder2) {
            HomePageBean.TodayLiveVideoBean bean1 = myList.get(position).get(TYPE2).get(1);
            HomePageBean.TodayLiveVideoBean bean2 = myList.get(position).get(TYPE2).get(2);
            Glide.with(context).load(bean1.getImageUrl()).into(((MyViewHolder2) holder).iv1_item3_img);
            Glide.with(context).load(bean2.getImageUrl()).into(((MyViewHolder2) holder).iv2_item3_img);
            ((MyViewHolder2) holder).tv1_item3_title.setText(bean1.getSubTitle());
            ((MyViewHolder2) holder).tv2_item3_title.setText(bean2.getSubTitle());
            ((MyViewHolder2) holder).tv1_item3_trailer.setText("#预告/" + bean1.getStartTime());
            ((MyViewHolder2) holder).tv2_item3_trailer.setText("#预告/" + bean2.getStartTime());
        }
    }

    @Override
    public int getItemCount() {
        return myList.size();
    }

    //预告大图
    static class MyViewHolder0 extends RecyclerView.ViewHolder {
        private ImageView iv_item1_trailerPic, iv_item1_trailerIcon;
        private TextView tv_item1_trailerTxt, tv_item1_TeacherName, tv_item1_TeacherPosition, tv_item1_Title, tx_item1_Detail;

        public MyViewHolder0(View itemView) {
            super(itemView);
            iv_item1_trailerPic = (ImageView) itemView.findViewById(R.id.iv_item1_trailerPic);
            iv_item1_trailerIcon = (ImageView) itemView.findViewById(R.id.iv_item1_trailerIcon);
            tv_item1_trailerTxt = (TextView) itemView.findViewById(R.id.tv_item1_trailerTxt);
            tv_item1_TeacherName = (TextView) itemView.findViewById(R.id.tv_item1_TeacherName);
            tv_item1_TeacherPosition = (TextView) itemView.findViewById(R.id.tv_item1_TeacherPosition);
            tv_item1_Title = (TextView) itemView.findViewById(R.id.tv_item1_Title);
            tx_item1_Detail = (TextView) itemView.findViewById(R.id.tx_item1_Detail);
        }
    }

    //直播
    static class MyViewHolder1 extends RecyclerView.ViewHolder {
        private ImageView iv_item2_trailerPic, iv_item2_MoveImg, iv_item2_trailerIcon;
        private TextView tv_item2_TeacherName, tv_item2_TeacherPosition, tv_item2_Title, tx_item2_Detail;

        public MyViewHolder1(View itemView) {
            super(itemView);
            iv_item2_trailerPic = (ImageView) itemView.findViewById(R.id.iv_item2_trailerPic);
            iv_item2_MoveImg = (ImageView) itemView.findViewById(R.id.iv_item2_MoveImg);
            iv_item2_trailerIcon = (ImageView) itemView.findViewById(R.id.iv_item2_trailerIcon);
            tv_item2_TeacherName = (TextView) itemView.findViewById(R.id.tv_item2_TeacherName);
            tv_item2_TeacherPosition = (TextView) itemView.findViewById(R.id.tv_item2_TeacherPosition);
            tv_item2_Title = (TextView) itemView.findViewById(R.id.tv_item2_Title);
            tx_item2_Detail = (TextView) itemView.findViewById(R.id.tx_item2_Detail);
        }
    }

    //预告小图
    static class MyViewHolder2 extends RecyclerView.ViewHolder {
        private ImageView iv1_item3_img, iv2_item3_img;
        private TextView tv1_item3_title, tv2_item3_title, tv1_item3_trailer, tv2_item3_trailer;

        public MyViewHolder2(View itemView) {
            super(itemView);
            iv1_item3_img = (ImageView) itemView.findViewById(R.id.iv1_item3_img);
            iv2_item3_img = (ImageView) itemView.findViewById(R.id.iv2_item3_img);
            tv1_item3_title = (TextView) itemView.findViewById(R.id.tv1_item3_title);
            tv2_item3_title = (TextView) itemView.findViewById(R.id.tv2_item3_title);
            tv1_item3_trailer = (TextView) itemView.findViewById(R.id.tv1_item3_trailer);
            tv2_item3_trailer = (TextView) itemView.findViewById(R.id.tv2_item3_trailer);
        }
    }
}
